package com.example.qihuibao.widget.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.example.qihuibao.R
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.widget.dialog.*

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/13
 *     desc   :
 */
class SettingLayout(ctx:Context) : FrameLayout(ctx) {
    private lateinit var activity:MainActivity
    private lateinit var root:View

    private var isModify = false

    init {
        if (ctx is MainActivity) {
            activity = ctx
        }
        root = LayoutInflater.from(ctx).inflate(R.layout.layout_setting, this, true)
        initView()
    }

    private fun initView() {
        root.findViewById<View>(R.id.tv_init).setOnClickListener {
            if (context is MainActivity) {
                (context as MainActivity).reInitDevice()
            }
        }
        root.findViewById<View>(R.id.tv_reset).setOnClickListener {
            YesOrNoDialog("警告", "确定恢复出厂设置，所有数据将被清除。") {
                if (it) {
                    activity.resetDevice()
                }
            }.show(activity.supportFragmentManager, "alarm")
        }
        root.findViewById<View>(R.id.tv_update_enterprise_Data).setOnClickListener {
            activity.updateData()
        }
        root.findViewById<View>(R.id.tv_update_other_Data).setOnClickListener {
            activity.updateOtherData()
        }
        root.findViewById<View>(R.id.tv_select_canteen).setOnClickListener {
            activity.chooseCanteen()
        }
        root.findViewById<View>(R.id.tv_select_meal).setOnClickListener {
            activity.chooseMeal()
        }
        root.findViewById<View>(R.id.tv_upload_offline_data).setOnClickListener {
            activity.uploadOfflineData()
        }
        //
        root.findViewById<View>(R.id.tv_enterprise_info_query).setOnClickListener {
//            EnterpriseInfoQueryDialog().show(activity.supportFragmentManager, "")
            (root as ViewGroup).addView(EnterpriseInfoLayout(context))
        }
        root.findViewById<View>(R.id.tv_department_info_query).setOnClickListener {
            (root as ViewGroup).addView(DepartmentInfoLayout(context))
        }
        root.findViewById<View>(R.id.tv_employ_info_query).setOnClickListener {
            (root as ViewGroup).addView(EmployInfoLayout(context))
        }
        root.findViewById<View>(R.id.tv_canteen_info_query).setOnClickListener {
            (root as ViewGroup).addView(CanteenInfoLayout(context))
        }
        root.findViewById<View>(R.id.tv_meal_info_1).setOnClickListener {
            (root as ViewGroup).addView(MealInfoLayout(context, "01"))
        }
        root.findViewById<View>(R.id.tv_meal_info_2).setOnClickListener {
            (root as ViewGroup).addView(MealInfoLayout(context, "02"))
        }
        root.findViewById<View>(R.id.tv_meal_info_3).setOnClickListener {
            (root as ViewGroup).addView(MealInfoLayout(context, "03"))
        }
        root.findViewById<View>(R.id.tv_meal_info_4).setOnClickListener {
            (root as ViewGroup).addView(MealInfoLayout(context, "04"))
        }
        root.findViewById<View>(R.id.tv_meal_info_5).setOnClickListener {
            (root as ViewGroup).addView(MealInfoLayout(context, "05"))
        }
        root.findViewById<View>(R.id.tv_meal1_fee_query).setOnClickListener {
            MealFeeQueryDialog("01").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal1_fee_add).setOnClickListener {
            MealFeeAddDialog("01").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal2_fee_query).setOnClickListener {
            MealFeeQueryDialog("02").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal2_fee_add).setOnClickListener {
            MealFeeAddDialog("02").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal3_fee_query).setOnClickListener {
            MealFeeQueryDialog("03").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal3_fee_add).setOnClickListener {
            MealFeeAddDialog("03").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal4_fee_query).setOnClickListener {
            MealFeeQueryDialog("04").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal4_fee_add).setOnClickListener {
            MealFeeAddDialog("04").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal5_fee_query).setOnClickListener {
            MealFeeQueryDialog("05").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_meal5_fee_add).setOnClickListener {
            MealFeeAddDialog("05").show(activity.supportFragmentManager, "")
        }
        root.findViewById<View>(R.id.tv_order_qry).setOnClickListener {
            (root as ViewGroup).addView(OrderInfoLayout(context))
        }
    }
}